package com.acxy.ants.core.plugin.sqlmap;

import com.acxy.ants.common.kit.JsonMap;
import com.acxy.ants.core.IBean;
import com.acxy.ants.core.plugin.db.Db;
import com.acxy.ants.resful.bean.Page;
import com.alibaba.fastjson.JSON;

import java.util.List;
import java.util.Map;

/**
 * Created by liushun on 2016/7/7.
 */
public class SqlKit{

    public static String getSql(String key) {
        return SqlXmlParser.getBoundSql(key, null);
    }

    public static String getSql(String key, Object param) {
        return SqlXmlParser.getBoundSql(key, param);
    }


    public static Long saveReturnKey(Db db, String statement, Map params) {
        SqlParams sql = SqlXmlParser.getPreparedStatement(statement, params);
        return db.insertReturnKey(sql.getSql(), sql.getParams());
    }

    public static void save(Db db, String statement, Map params) {
        SqlParams sql = SqlXmlParser.getPreparedStatement(statement, params);
        db.insert(sql.getSql(), sql.getParams());
    }

    public static int delete(Db db, String statement, Map params) {
        SqlParams sql = SqlXmlParser.getPreparedStatement(statement, params);
        return db.delete(sql.getSql(), sql.getParams());
    }

    public static Long count(Db db, String statement, Map params) {
        SqlParams sql = SqlXmlParser.getPreparedStatement(statement, params);
        return db.query(sql.getSql(), sql.getParams()).getLong("count");
    }

    public static <T extends IBean> T find(Db db, String statement, Map params, T obj) {
        SqlParams sql = SqlXmlParser.getPreparedStatement(statement, params);
        return db.query(sql.getSql(), obj, sql.getParams());
    }
    public static <T extends IBean> T find(Db db, String statement, T obj){
        return find(db, statement, null, obj);
    }
    public static JsonMap find(Db db, String statement, Map params) {
        SqlParams sql = SqlXmlParser.getPreparedStatement(statement, params);
        System.err.println(sql.getSql());
        System.err.println(JSON.toJSONString(sql.getParams()));
        return db.query(sql.getSql(), sql.getParams());
    }
    public static JsonMap find(Db db, String statement){
        return find(db, statement, JsonMap.newJsonMap());
    }


    public static <T extends IBean> List<T> list(Db db, String statement, Map params, Class<T> cls) {
        SqlParams sql = SqlXmlParser.getPreparedStatement(statement, params);
        return db.list(sql.getSql(), cls, sql.getParams());
    }
    public static <T extends IBean> List<T> list(Db db, String statement, Class<T> cls) {
        return list(db, statement, null, cls);
    }
    public static List<JsonMap> list(Db db, String statement, Map params) {
        SqlParams sql = SqlXmlParser.getPreparedStatement(statement, params);
        return db.list(sql.getSql(), sql.getParams());
    }
    public static List<JsonMap> list(Db db, String statement) {
        SqlParams sql = SqlXmlParser.getPreparedStatement(statement, null);
        return db.list(sql.getSql(), sql.getParams());
    }

    public static int update(Db db, String statement, Map params) {
        SqlParams sql = SqlXmlParser.getPreparedStatement(statement, params);
        return db.update(sql.getSql(), sql.getParams());
    }

    public static <T extends IBean> Page page(Db db, String statement, Map params, Class<T> cls, int index, int size) {
        SqlParams sql = SqlXmlParser.getPreparedStatement(statement, params);
        return db.page(sql.getSql(), cls, index, size, sql.getParams());
    }
    public static <T extends IBean> Page page(Db db, String statement, Class<T> cls, int index, int size) {
        return page(db, statement, null, cls, index, size);
    }

    public static Page page(Db db, String statement, Map params, int index, int size) {
        SqlParams sql = SqlXmlParser.getPreparedStatement(statement, params);
        return db.page(sql.getSql(), index, size, sql.getParams());
    }
    public static Page page(Db db, String statement, int index, int size) {
        return page(db, statement, JsonMap.newJsonMap(), index, size);
    }
}